﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Thaneros
{
    public class Bunny : Enemy
    {
        enum State
        {
            Idle = 0,
            Jumping = 1,
        };

        State state;
        public Vector2 speed = new Vector2(0, 0);
        float gravityAcc = .5f; // não pode ser menor que 1!
        bool onGround = false;
        double timer = 0;
        int idleTime = 2000;
        int speedx = -5;

        public Bunny(PlayScreen ps)
            : base(ps, Engine.Load<Texture2D>("Images\\Enemies\\bunny"))
        {
            position += ps.c.position + new Vector2(1000, 0);
            state = State.Jumping;
        }

        public override void Update(GameTime gameTime)
        {
            if (!alive) return;
            switch (state)
            {
                case State.Idle:
                    if (!onGround) break;
                    timer += gameTime.ElapsedGameTime.TotalMilliseconds;
                    if (timer > idleTime)
                    {
                        timer = 0;
                        state = State.Jumping;
                        speedx = 5 * (position.X > ps.c.position.X ? -1 : 1);
                    }
                    break;
                case State.Jumping:
                    onGround = false;
                    speed.Y -= 15;
                    state = State.Idle;
                    ps.soundBank.PlayCue("Bunny_Jump");
                    break;
                default:
                    break;
            }
            if (onGround) speed.X = 0;
            else speed.X = speedx;
            UpdatePosition(gameTime);
            if (Collision(ps.c))
            {
                alive = false;
                ps.c.HP -= 5;
            }
        }

        public void UpdatePosition(GameTime gameTime)
        {
            speed.Y += gravityAcc; // update gravity

            // update Position.X
            if (speed.X != 0)
            {
                position.X += speed.X;
                float dH = ps.bm.CollisionH(this, speed.X < 0, true);
                if (dH != 0) position.X += dH;
                speed.X = 0;
            }

            // update Position.Y
            position.Y += speed.Y;
            float dV = ps.bm.CollisionV(this, speed.Y < 0, !onGround);
            if (dV != 0)
            {
                position.Y += dV;
                if (speed.Y > 0 && !onGround)
                {
                    ps.soundBank.PlayCue("Bunny_Fall");
                    onGround = true; // se estava caindo (não subindo), então agora está no chão pq colidiu (com o chão)
                }
                speed.Y = 0;
            }
            else onGround = false;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (ps.c == null || sprite == null) return;

            drawPosition = position - ps.c.position + ps.c.drawPosition;
            spriteBatch.Draw(sprite, drawPosition, sourceRectangle, Color.White, rotation, origin, scale, position.X > ps.c.position.X ? SpriteEffects.None : SpriteEffects.FlipHorizontally, 0);
        }
    }
}
